<template>
  <view class="activation-container">
    <!-- 页面头部 -->
    <view class="page-header">
      <view class="header-left" @click="goBack">
        <text class="back-icon">←</text>
        <text class="back-text">返回</text>
      </view>
      <text class="header-title">使用激活码加入家谱</text>
      <view class="header-right"></view>
    </view>

    <!-- 激活码表单 -->
    <view class="form-container">
      <view class="form-section">
        <view class="section-title">激活信息</view>

        <view class="form-item">
          <text class="label">激活码</text>
          <input
            v-model="activationForm.activationCode"
            class="input"
            placeholder="请输入家谱激活码"
            maxlength="20"
          />
        </view>
      </view>

      <view class="form-section">
        <view class="section-title">家庭成员信息</view>

        <view class="form-item">
          <text class="label">爷爷姓名</text>
          <input
            v-model="activationForm.grandpaName"
            class="input"
            placeholder="请输入爷爷的姓名"
          />
        </view>

        <view class="form-item">
          <text class="label">父亲姓名</text>
          <input v-model="activationForm.fatherName" class="input" placeholder="请输入父亲的姓名" />
        </view>

        <view class="form-item">
          <text class="label">本人姓名</text>
          <input v-model="activationForm.selfName" class="input" placeholder="请输入您的姓名" />
        </view>
      </view>

      <!-- 说明信息 -->
      <view class="info-section">
        <view class="info-header">
          <text class="info-icon">💡</text>
          <text class="info-title">使用说明</text>
        </view>
        <view class="info-content">
          <text class="info-text">1. 激活码由家谱管理员提供</text>
          <text class="info-text">2. 请确保填写准确的家族成员姓名</text>
          <text class="info-text">3. 激活码使用后将被消耗</text>
          <text class="info-text">4. 加入成功后可直接查看家谱</text>
        </view>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="bottom-actions">
      <button class="btn btn-default" @click="goBack">取消</button>
      <button class="btn btn-primary" :disabled="!canSubmit" @click="submitActivation">
        确认加入
      </button>
    </view>
  </view>
</template>

<script>
import FamilyAPI from "@/api/genealogy/family.ts";

export default {
  name: "GenealogyActivation",
  data() {
    return {
      familyId: null,
      activationForm: {
        familyId: null,
        activationCode: "",
        grandpaName: "",
        fatherName: "",
        selfName: "",
      },
    };
  },

  computed: {
    canSubmit() {
      return (
        this.activationForm.activationCode.trim() &&
        this.activationForm.grandpaName.trim() &&
        this.activationForm.fatherName.trim() &&
        this.activationForm.selfName.trim()
      );
    },
  },

  onLoad(options) {
    this.familyId = options.familyId;
    this.activationForm.familyId = this.familyId;
  },

  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },

    // 提交激活码加入申请
    submitActivation() {
      if (!this.canSubmit) {
        uni.showToast({
          title: "请填写完整信息",
          icon: "none",
        });
        return;
      }

      uni.showLoading({
        title: "验证中...",
      });

      FamilyAPI.joinByActivationCode(this.activationForm)
        .then(() => {
          uni.hideLoading();
          uni.showToast({
            title: "成功加入家谱!",
            icon: "success",
          });

          // 延迟返回，让用户看到成功提示
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        })
        .catch((error) => {
          uni.hideLoading();
          uni.showToast({
            title: "加入失败，请检查激活码是否正确",
            icon: "none",
          });
          console.error("加入家谱失败:", error);
        });
    },
  },
};
</script>

<style scoped>
.activation-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-bottom: 2rpx solid #eee;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.back-icon {
  font-size: 32rpx;
  color: #333;
}

.back-text {
  font-size: 28rpx;
  color: #333;
}

.header-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.header-right {
  width: 80rpx;
}

.form-container {
  padding: 30rpx;
}

.form-section {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 15rpx;
}

.input {
  width: 100%;
  padding: 20rpx;
  border: 2rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.input:focus {
  border-color: #409eff;
}

.info-section {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.info-header {
  display: flex;
  align-items: center;
  gap: 10rpx;
  margin-bottom: 20rpx;
}

.info-icon {
  font-size: 32rpx;
}

.info-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
}

.info-content {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.info-text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.5;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  gap: 20rpx;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-top: 2rpx solid #eee;
  box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.btn {
  flex: 1;
  padding: 20rpx;
  border-radius: 8rpx;
  font-size: 28rpx;
  border: none;
  text-align: center;
}

.btn-primary {
  background-color: #409eff;
  color: #fff;
}

.btn-default {
  background-color: #fff;
  color: #333;
  border: 2rpx solid #ddd;
}

.btn:disabled {
  opacity: 0.5;
  pointer-events: none;
}
</style>
